require([ "require-config" ], function() {
    require([ 'jquery', 'validate' ], function() {
    	jQuery.validator.addMethod("Accountcheck",function(value, element, params) {
			var newPasswd = $('#memberAccount').val()
			if(value.match(/^\w+$/)){   
				return true;  
			} 
			return false;
    	})
    	jQuery.validator.addMethod("pwdcheck",function(value, element, params) {
			var newPasswd = $('#memberPassword').val()
			if(value.match(/^\w+$/)){   
				return true;  
			} 
			return false;
    	})
    	jQuery.validator.addMethod("phonecheck",function(value, element, params) {
			var newPasswd = $('#memberPhone').val()
			if(value.match(/(^0{0,1}1[3|4|5|6|7|8|9][0-9]{9}$)/)){   
				return true;  
			} 
			return false;
    	})
    	jQuery.validator.addMethod("emailcheck",function(value, element, params) {
			var newPasswd = $('#memberEmail').val()
			if(value.match(/^[_\.0-9a-z-]+@([0-9a-z][0-9a-z-]+\.){1,4}[a-z]{2,3}$/)){   
				return true;  
			} 
			return false;
    	})
        $('form').validate({
            rules : {
            	memberAccount: {
                    required: true,
                    Accountcheck :true,
                    rangelength: [6,30]
                },
                memberPassword: {
                    required: true,
                    pwdcheck:true,
                    rangelength: [6,30]
                },
                memberName: {
                    required: true
                },
                memberPhone: {
                    required: true,
                    phonecheck:true,
                    rangelength: [6,30]
                },
                memberEmail: {
                    required: true,
                    emailcheck :true,
                    rangelength: [6,30]
                },
                memberLeader: {
                    required: true
                }
            },
            messages : {
            	memberAccount: {
                    required: '用户名不能为空',
                    Accountcheck: '用户名只能是字母、数字和下划线',
                    rangelength : "用户名长度必须在6-30内"
                },
                memberPassword: {
            		required: '密码不能为空',
            		pwdcheck : '密码只能是字母、数字和下划线',
            		rangelength : "密码长度必须在6-30内"
            	},
            	memberName: {
            		required: '密码不能为空'
            	},
            	memberPhone: {
            		required: '手机号不能为空',
            		phonecheck:'手机号格式有误'
            	},
            	memberEmail: {
            		required: '邮箱不能为空',
            		emailcheck:'邮箱格式有误'
            	},
            	memberLeader: {
            		required: '密码不能为空'
            	}
            },
            submitHandler : function(form) {
                $.ajax({
                    url: 'add',
                    type: 'POST',
                    data: {
                        'memberAccount': $('#memberAccount').val(),
                        'memberPassword': $('#memberPassword').val(),
                        'memberName': $('#memberName').val(),
                        'memberPhone': $('#memberPhone').val(),
                        'memberEmail': $('#memberEmail').val(),
                        'memberLeader': $('#memberLeader').val(),
                        'memberNumStatus': $('#memberNumStatus').val(),	
                        'memberGroup': $('#memberGroup').val()
                    },
                    dataType: 'json',
                    beforeSend: function() {
                        $("#frm_submit").attr("disabled", true);
                    },
                    success: function(response) {
                        if (true == response.success) {
                        	alert(response.message);
                            window.location.href = 'list';
                        } else {
                            alert(response.message);
                        }
                        $("#frm_submit").attr("disabled", false);
                    },
                    error: function(response) {
                    	alert(response.message);
                        $("#frm_submit").attr("disabled", false);
                    }
                });
            }
        });
    });
});